const { pool } = require('./db');

class StaffInfoDao {
    // 获取指定用户的考勤记录
    async getAttendanceByUserId(userId) {
        try {
            const [rows] = await pool.query(
                'SELECT atd_id, atd_absentdays, atd_money, atd_date FROM attendance WHERE atd_user_id = ?',
                [userId]
            );
            return rows;
        } catch (error) {
            console.error('获取考勤记录失败:', error);
            throw error;
        }
    }

    // 添加考勤记录
    async addAttendance(attendance) {
        try {
            const [result] = await pool.query(
                'INSERT INTO attendance (atd_user_id, atd_absentdays, atd_money, atd_date) VALUES (?, ?, ?, ?)',
                [attendance.user_id, attendance.atd_absentdays, attendance.atd_money, attendance.atd_date]
            );
            return result.insertId;
        } catch (error) {
            console.error('添加考勤记录失败:', error);
            throw error;
        }
    }

    // 更新考勤记录
    async updateAttendance(id, attendance) {
        try {
            const [result] = await pool.query(
                'UPDATE attendance SET atd_absentdays = ?, atd_money = ?, atd_date = ? WHERE atd_id = ?',
                [attendance.atd_absentdays, attendance.atd_money, attendance.atd_date, id]
            );
            return result.affectedRows > 0;
        } catch (error) {
            console.error('更新考勤记录失败:', error);
            throw error;
        }
    }

    // 删除考勤记录
    async deleteAttendance(id) {
        try {
            const [result] = await pool.query(
                'DELETE FROM attendance WHERE atd_id = ?',
                [id]
            );
            return result.affectedRows > 0;
        } catch (error) {
            console.error('删除考勤记录失败:', error);
            throw error;
        }
    }
}

module.exports = new StaffInfoDao();